package Accessors;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import utils.RESTfulConnect;
import utils.URLTools;

import Callbacks.AbstractCallback;
import Enums.RFAT;
import Models.BrewView;
import Presenter.BrewViewPresenter;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

@SuppressWarnings("rawtypes")
public class BrewViewAccessor extends AbstractAccessor<BrewView,BrewViewPresenter,AbstractCallback> {
	private final String NAME = "brewview";
	private final String DIALOG = "Brew View";
	
	private final static BrewViewPresenter presenter = new BrewViewPresenter();
	private AbstractCallback callback = null;
	
	//Modal dialog variable.
	ProgressDialog modalDialog = null;
	
	public BrewViewAccessor(AbstractCallback callback){
		super(presenter, callback);
		this.callback = callback;
	}
	
	//Public access functions.
	public void getAllItems(final Context context){
		String dialog = "Loading " + DIALOG + " List...";
		String rfString = NAME;
		super.getAllItems(context, dialog, rfString);
	}
	
	public void getAllItemsInProduction(final Context context){
		String dialog = "Loading " + DIALOG + " List...";
		final String rfString = NAME + "/runNamedQuery/BrewView.findAllInProduction";
		super.getAllItems(context, dialog, rfString);
	}
	
	public void getAllPackageableBrews(final Context context){
		String dialog = "Loading " + DIALOG + " List...";
		final String rfString = NAME + "/runNamedQuery/BrewView.getPackagableBrews";
		super.getAllItems(context, dialog, rfString);
	}
	
	public void getItem(final int id, final Context context){
		String dialog = "Loading " + DIALOG + "...";
		String rfString = NAME + "/" + Integer.toString(id);
		super.getItem(context, dialog, rfString);
	}
	
	public void doItemUpdate(final BrewView item, final Context context){
		String dialog = "Updating " + DIALOG + "...";
		String rfString = NAME;
		super.doItemUpdate(item, context, dialog, rfString);
	}
	
	public void doItemInsert(final BrewView item, final Context context){
		String dialog = "Inserting " + DIALOG + "...";
		String rfString = NAME;
		super.doItemInsert(item, context, dialog, rfString);
	}

	/***************************************************************************
	 * Code to create a new Schedule.
	 * @param beerID
	 * @param vol
	 * @param brewDate
	 * @param context
	 ***************************************************************************/
	public void createNewBrewSchedule(final int beerID, final double vol, final Date brewDate, final Context context){
		String dialog = "Inserting " + DIALOG + "...";
		String rfString = NAME + "/createNewBrewSchedule/" + Integer.toString(beerID) + "/" + Double.toString(vol) + "/" + brewDate.toString();
		rfString = rfString.replaceAll(" ", "%20");
		doNewBrewInsert(context, dialog, rfString);
	}
	
	public void doNewBrewInsert(final Context context, final String dialog, final String str){
		modalDialog = ProgressDialog.show(context, null, dialog);
		final String strStart = URLTools.getURLString(context);
		new doInsertNewBrewTask().execute(strStart+str, URLTools.getUSERString(context), URLTools.getPASSString(context));
	}
	
	private class doInsertNewBrewTask extends AsyncTask<String, Void, String> {
    	@Override
        protected String doInBackground(String... urls) {
            try {
                return RESTfulConnect.postResourceNoJson(urls[0], urls[1], urls[2]);
            } catch (IOException e) {
            	e.printStackTrace();
            	return "Connection Error";
            }
        }
    	@SuppressWarnings("unchecked")
		@Override
        protected void onPostExecute(String result) {
    		try{
    			BrewViewPresenter presenter = new BrewViewPresenter();
	    		modalDialog.dismiss();
	    		ArrayList<BrewView> items = new ArrayList<BrewView>();
	    		if(result.subSequence(0, 2).equals("<?")){
	    			BrewView t = (BrewView)presenter.tryJSONParse(result);
		    		callback.callback(t, RFAT.INSERT, items);
	    		} else {
	    			JSONObject json = new JSONObject(result);
	    			BrewView t = (BrewView)presenter.createItemFromJSONObject(json);
		    		callback.callback(t, RFAT.INSERT, items);
	    		}
    		} catch(JSONException ex){
    			ex.printStackTrace();
    		} 
        }
    }
}
